package com.sgb.kjwl.utils;


import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.swgk.core.util.ScreenUtil;

import java.util.Objects;


/**
 * 会员侧滑栏使用
 * https://www.jianshu.com/p/85172c70bb98
 */
public class GridItemDecoration extends RecyclerView.ItemDecoration {
    //
    private int mTotalItems;//总Item数
    private int mSpanCount;//总列数
    private int space;

    public GridItemDecoration(int space) {
        this.space = ScreenUtil.dip2px(space);
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        if (0 == mTotalItems)
            mTotalItems = Objects.requireNonNull(parent.getAdapter()).getItemCount();
        if (0 == mSpanCount) {
            RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
            //判断是否为GridLayoutManager
            if (layoutManager instanceof GridLayoutManager) {
                GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
                mSpanCount = gridLayoutManager.getSpanCount();
            } else {
                mSpanCount = 1;
            }
        }
        //在源码中有一个过时的方法，里面有获取当前ItemPosition
        int currentItemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
        //
        if (!isLastRow(currentItemPosition, mTotalItems, mSpanCount))
            outRect.bottom = space;
        else
            outRect.bottom = 0;
        //
        if (!isLastColumn(currentItemPosition, mSpanCount)) {
            outRect.right = space;
        } else {
            outRect.right = 0;
        }

    }

   /* @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        for (int i = 0; i < parent.getChildCount(); i++) {
            drawHorizontalDecoration(c, parent.getChildAt(i));
            drawVerticalDecoration(c, parent.getChildAt(i));
        }
    }

    private void drawHorizontalDecoration(Canvas canvas, View childView) {
        int currentItemPosition = ((RecyclerView.LayoutParams) childView.getLayoutParams()).getViewLayoutPosition();
        if (isLastRow(currentItemPosition, mTotalItems, mSpanCount)) {
            return;
        }
        //
        Rect rect = new Rect(0, 0, 0, 0);

        rect.top = childView.getBottom();
        rect.bottom = rect.top + mDrawable.getIntrinsicWidth();
        rect.left = childView.getLeft();
        rect.right = childView.getRight() + mDrawable.getIntrinsicWidth();

        mDrawable.setBounds(rect);
        mDrawable.draw(canvas);
    }

    private void drawVerticalDecoration(Canvas canvas, View childView) {
        Rect rect = new Rect(0, 0, 0, 0);

        rect.top = childView.getTop();
        rect.bottom = childView.getBottom();
        rect.left = childView.getRight();
        rect.right = rect.left + mDrawable.getIntrinsicWidth();

        mDrawable.setBounds(rect);
        mDrawable.draw(canvas);
    }*/

    private boolean isLastRow(int currentItemPosition, int totalItems, int spanCount) {
        boolean result = false;
        int rowCount = 0;

        if (0 == totalItems % spanCount) {
            rowCount = totalItems / spanCount;
        } else {
            rowCount = totalItems / spanCount + 1;
        }
        if ((currentItemPosition + 1) > (rowCount - 1) * spanCount)
            result = true;

        return result;
    }

    private boolean isLastColumn(int currentItemPosition, int spanCount) {
        boolean result = false;
        if (0 == (currentItemPosition + 1) % spanCount)
            result = true;
        return result;
    }
}

